﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

namespace BDSim
{
    public class GroundDataProvider : BDSimDataProvider
   {
       private GroundSegment _Parent;
       
       public GroundSegment Parent
       {
           get { return _Parent; }
           set { _Parent = value; }
       }       
       
       #region 命令定义

       /// <summary>
       /// 添加GPS地面段
       /// </summary>
       MenuCommand AddGPSGroundSegmentCmd = new MenuCommand();

       /// <summary>
       /// 添加北斗地面段
       /// </summary>
       MenuCommand AddBDGroundSegmentCmd = new MenuCommand();

       /// <summary>
       /// 添加Glonass地面段
       /// </summary>
       MenuCommand AddGLONASSGroundSegmentCmd = new MenuCommand();

       /// <summary>
       /// 添加Galileo地面段
       /// </summary>
       MenuCommand AddGALILEOGroundSegmentCmd = new MenuCommand();

       /// <summary>
       /// 清空按钮
       /// </summary>
       MenuCommand DeleteCmd = new MenuCommand();

       #endregion

       #region 命令集合初始化

       public override void DefineCommand()
       {
           AddBDGroundSegmentCmd.Header = "BDS地面段";
           AddBDGroundSegmentCmd.DefineExcuteCommand(AddBDGroundSegmentFun_Execute, AddBDGroundSegmentFun_CanExecute);
           MenuCommandList.Add(AddBDGroundSegmentCmd);

           AddGPSGroundSegmentCmd.Header = "GPS地面段";
           AddGPSGroundSegmentCmd.DefineExcuteCommand(AddGPSGroundSegmentFun_Execute, AddGPSGroundSegmentFun_CanExecute);
           MenuCommandList.Add(AddGPSGroundSegmentCmd);

           AddGLONASSGroundSegmentCmd.Header = "GLONASS地面段";
           AddGLONASSGroundSegmentCmd.DefineExcuteCommand(AddGLONASSGroundSegmentFun_Execute, AddGLONASSGroundSegmentFun_CanExecute);
           MenuCommandList.Add(AddGLONASSGroundSegmentCmd);

           AddGALILEOGroundSegmentCmd.Header = "GALILEO地面段";
           AddGALILEOGroundSegmentCmd.DefineExcuteCommand(AddGALILEOGroundSegmentFun_Execute, AddGALILEOGroundSegmentFun_CanExecute);
           MenuCommandList.Add(AddGALILEOGroundSegmentCmd);

           DeleteCmd.Header = "清空";
           DeleteCmd.DefineExcuteCommand(DeleteFun);
           MenuCommandList.Add(DeleteCmd); 

       }

       #endregion

       #region 命令执行内容
        void AddStationCollection(SubGroundSegment SubG)
        {
            StationCollection StationCollection_Master = new StationCollection();
            StationCollection_Master.Parent = SubG;
           // StationCollection_Master.Parent.Parent = Parent;
            StationCollection_Master.Type = EStationType.MasterStation;
            StationCollection_Master.Name = "主控站";
            SubG.GroundList.Add(StationCollection_Master);

            StationCollection StationCollection_Inject = new StationCollection();
            StationCollection_Inject.Parent = SubG;
           // StationCollection_Inject.Parent.Parent = Parent;
            StationCollection_Inject.Type = EStationType.InjectionStation;
            StationCollection_Inject.Name = "注入站";
            SubG.GroundList.Add(StationCollection_Inject);

            StationCollection StationCollection_Monitor = new StationCollection();
            StationCollection_Monitor.Parent = SubG;
            //StationCollection_Monitor.Parent.Parent = Parent;
            StationCollection_Monitor.Type = EStationType.MonitorStation;
            StationCollection_Monitor.Name = "监测站";
            SubG.GroundList.Add(StationCollection_Monitor);
     
        }



       /// 添加GPS地面段的执行方法
       void AddGPSGroundSegmentFun_Execute(object param)
       {
           SubGroundSegment GPSGroundSegment = new SubGroundSegment();
           GPSGroundSegment.Type = EConstellationType.GPS;
           GPSGroundSegment.Name ="GPS地面段" ;
           GPSGroundSegment.Parent = Parent;
           AddStationCollection(GPSGroundSegment);

           _Parent.SubGroundSegmentList.Add(GPSGroundSegment);
 
       }

       /// 添加GPS地面段的使能控制
       bool AddGPSGroundSegmentFun_CanExecute(object param)
       {
           bool flag = false;
           if (BDSimObject.GlobalStatus==ESimStatus.UnInited || BDSimObject.GlobalStatus ==ESimStatus.End )
           {
               flag = true;
               foreach (var subg in Parent.SubGroundSegmentList)
               {
                   if (subg.Type==EConstellationType.GPS)
                   {
                       flag = false;
                       break;
                   }
               }
               
           }
           return flag;
 
       }

       void AddBDGroundSegmentFun_Execute(object param)
       {
           SubGroundSegment BDGroundSegment = new SubGroundSegment() { Name = "BDS地面段"};
           BDGroundSegment.Type = EConstellationType.BD;
           BDGroundSegment.Parent=Parent;
           AddStationCollection(BDGroundSegment);
           _Parent.SubGroundSegmentList.Add(BDGroundSegment);

       }
       bool AddBDGroundSegmentFun_CanExecute(object param)
       {
           bool flag = false;
           if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
           {
               flag = true;
               foreach (var subg in Parent.SubGroundSegmentList)
               {
                   if (subg.Type == EConstellationType.BD)
                   {
                       flag = false;
                       break;
                   }
               }
           }
           return flag;
       }

       void AddGLONASSGroundSegmentFun_Execute(object param)
       {
           SubGroundSegment GLONASSGroundSegment = new SubGroundSegment() { Name = "GLONASS地面段" };
           GLONASSGroundSegment.Type = EConstellationType.GLONASS;
           GLONASSGroundSegment.Parent = Parent;
           AddStationCollection(GLONASSGroundSegment);

           _Parent.SubGroundSegmentList.Add(GLONASSGroundSegment);

       }
       bool AddGLONASSGroundSegmentFun_CanExecute(object param)
       {
           bool flag = false;
           if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
           {
               flag = true;
               foreach (var subg in Parent.SubGroundSegmentList)
               {
                   if (subg.Type == EConstellationType.GLONASS)
                   {
                       flag = false;
                       break;
                   }
               }
           }
           return flag;
       }

       void AddGALILEOGroundSegmentFun_Execute(object param)
       {
           SubGroundSegment GALILEOGroundSegment = new SubGroundSegment() { Name = "GALILEO地面段" };
           GALILEOGroundSegment.Type = EConstellationType.GALILEO;
           GALILEOGroundSegment.Parent = Parent;
           AddStationCollection(GALILEOGroundSegment);

           _Parent.SubGroundSegmentList.Add(GALILEOGroundSegment);

       }
       bool AddGALILEOGroundSegmentFun_CanExecute(object param)
       {
           bool flag = false;
           if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
           {
               flag = true;
               foreach (var subg in Parent.SubGroundSegmentList)
               {
                   if (subg.Type == EConstellationType.GALILEO)
                   {
                       flag = false;
                       break;
                   }
               }
           }
           return flag;
       }

       void DeleteFun(object param)
       {
           Parent.SubGroundSegmentList.Clear();
       }
       #endregion
   }
}
